package com;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import tool.*;
@WebServlet(name = "ques_add")
public class ques_add extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        question question=new question(null,request.getParameter("title"),
                request.getParameter("A"),
                request.getParameter("B"),
                request.getParameter("C"),
                request.getParameter("D"),
                request.getParameter("answer"));
        try {
            connection=jdbctool.getconection();
            statement=connection.prepareStatement("insert into `servlet`.`question`\n" +
                    "            (`title`,\n" +
                    "             `A`,\n" +
                    "             `B`,\n" +
                    "             `C`,\n" +
                    "             `D`,\n" +
                    "             `answer`)\n" +
                    "values (?,\n" +
                    "        ?,\n" +
                    "        ?,\n" +
                    "        ?,\n" +
                    "        ?,\n" +
                    "        ?)");
            statement.setString(1,question.getTitle());
            statement.setString(2,question.getA());
            statement.setString(3,question.getB());
            statement.setString(4,question.getC());
            statement.setString(5,question.getD());
            statement.setString(6,question.getAnswer());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbctool.close(connection,statement,resultSet);
        }
        PrintWriter out=response.getWriter();
        out.write("插入成功！！");
    }
}
